//
//  WelcomeViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/7/2.
//

#import "WelcomeViewController.h"
#import "GuideViewController.h"
#import "MainTabBarController.h"
#import "HomeViewController.h"
#import <Masonry/Masonry.h>

@interface WelcomeViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, strong) UIButton *skipButton;

@end

@implementation WelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    // 设置渐变背景
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.view.bounds;
    gradientLayer.colors = @[
        (id)[UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0].CGColor,
        (id)[UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0].CGColor
    ];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
    
    // 滚动视图
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    // 内容视图
    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentView];
    
    // Logo图片
    self.logoImageView = [[UIImageView alloc] init];
    self.logoImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.logoImageView.image = [UIImage imageNamed:@"spaiamge"];
    self.logoImageView.layer.cornerRadius = self.logoImageView.frame.size.width/2;
    [self.contentView addSubview:self.logoImageView];
    
    // 标题
    self.titleLabel = [[UILabel alloc] init];
//    self.titleLabel.text = @"欢迎使用吉时黄历";
    self.titleLabel.text = @"欢迎使用生日农历记录";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:28];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.titleLabel];
    
    // 副标题
    self.subtitleLabel = [[UILabel alloc] init];
    self.subtitleLabel.text = @"生日农历记录，提前提醒，送上祝福";
//    self.subtitleLabel.text = @"传承千年智慧，助您趋吉避凶，把握人生每一天";
    self.subtitleLabel.font = [UIFont systemFontOfSize:16];
    self.subtitleLabel.textColor = [UIColor colorWithRed:0.996 green:0.988 blue:0.749 alpha:1.0];
    self.subtitleLabel.textAlignment = NSTextAlignmentCenter;
    self.subtitleLabel.numberOfLines = 0;
    [self.contentView addSubview:self.subtitleLabel];
    
    // 开始体验按钮
    self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.startButton setTitle:@"开始体验" forState:UIControlStateNormal];
    [self.startButton setTitleColor:[UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0] forState:UIControlStateNormal];
    self.startButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.startButton.backgroundColor = [UIColor whiteColor];
    self.startButton.layer.cornerRadius = 8;
    [self.startButton addTarget:self action:@selector(startButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.startButton];
    
    // 跳过引导按钮
    self.skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.skipButton setTitle:@"跳过引导" forState:UIControlStateNormal];
    [self.skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.skipButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.skipButton.layer.borderWidth = 2;
    self.skipButton.layer.borderColor = [UIColor whiteColor].CGColor;
    self.skipButton.layer.cornerRadius = 8;
    [self.skipButton addTarget:self action:@selector(skipButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.skipButton];
    [self setupConstraints];
}

- (void)setupConstraints {
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.height.greaterThanOrEqualTo(self.scrollView);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(120);
        make.centerX.equalTo(self.contentView);
        make.width.height.mas_equalTo(128);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoImageView.mas_bottom).offset(32);
        make.left.right.equalTo(self.contentView).inset(32);
        make.height.mas_equalTo(40);
    }];
    
    [self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(16);
        make.left.right.equalTo(self.contentView).inset(32);
    }];
    
    [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subtitleLabel.mas_bottom).offset(48);
        make.left.right.equalTo(self.contentView).inset(32);
        make.height.mas_equalTo(48);
    }];
    
    [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.startButton.mas_bottom).offset(16);
        make.left.right.equalTo(self.contentView).inset(32);
        make.height.mas_equalTo(48);
        make.bottom.equalTo(self.contentView).offset(-50);
    }];
}

- (UIImage *)createLogoImage {
    // 创建一个简单的Logo图片
    CGSize size = CGSizeMake(128, 128);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 绘制圆形背景
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillEllipseInRect(context, CGRectMake(4, 4, 120, 120));
    
    // 绘制渐变边框
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.996 green:0.988 blue:0.749 alpha:1.0].CGColor);
    CGContextSetLineWidth(context, 3);
    CGContextStrokeEllipseInRect(context, CGRectMake(4, 4, 120, 120));
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)startButtonTapped {
    // 标记已启动过
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasLaunchedBefore"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    GuideViewController *guideVC = [[GuideViewController alloc] init];
    guideVC.modalPresentationStyle = UIModalPresentationFullScreen;
    guideVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:guideVC animated:YES completion:nil];
}

- (void)skipButtonTapped {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasLaunchedBefore"];
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    homeVC.modalPresentationStyle = UIModalPresentationFullScreen;
    homeVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:homeVC animated:YES completion:nil];
}

@end 
